// pages/console/console.js
const consoleIMG = require('./consoleIMG.js');
import JCAPI from "../../utils/JCAPI/JCAPI";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shangpin: [],
    imgurl: "",
    consoleTab: false,
    count4danhao: 1,
    printTab: false,
    Plist: [],
    nowPrint: [],
  },
  clickRadio: function (params) {
    console.log(params.detail.value);
    this.setData({
      imgurl: params.detail.value
    })
  },
  log4img: function (params) {
    if (this.data.imgurl != '') {
      this.setData({
        consoleTab: true
      })
    }
  },

  consoleTabCandel: function () {
    // do something
    this.setData({
      consoleTab: false
    })
  },

  /**
    *  点击确认
    */
  consoleTabConfirm: function () {
    // do something
    var that = this
    this.setData({
      consoleTab: false
    })
    wx.showLoading({  // 显示加载中loading效果 
      title: "查找设备中",
      mask: true  //开启蒙版遮罩
    });
    consoleIMG.linkPrint().then(res => {
      that.setData({
        Plist: res,
        printTab: true,
        nowPrint: getApp().globalData.nowPrint != null ? getApp().globalData.nowPrint : []
      })
      wx.hideLoading()
    })
  },

  printTabCandel: function () {
    // do something
    this.setData({
      printTab: false
    })
  },

  /**
    *  点击确认
    */
  printTabConfirm: function () {
    // do something
    var print2p = this.data.nowPrint != null ? this.data.nowPrint : null;
    if (this.data.nowPrint != null) {
      this.setData({
        printTab: false
      })
      getApp().globalData.nowPrint = this.data.nowPrint != null ? this.data.nowPrint : this.data.Plist[0];
      wx.showLoading({  // 显示加载中loading效果 
        title: "打印中（" + app.globalData.printingCount + "/" + this.data.count4danhao + "）",
        mask: true  //开启蒙版遮罩
      });
      consoleIMG.consoleIMG(this.data.imgurl, this.data.count4danhao, print2p, this)
        .then((result) => {
          console.log(result);
          wx.hideLoading();
          wx.showToast({
            title: result,
            icon: result == "打印完成" ? "success" : "error",
          })
        });
    } else {
      wx.showToast({
        title: '请选择打印机',
        icon: 'error'
      })
    }
  },
  valuechange: function (params) {
    this.setData({
      count4danhao: params.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      shangpin: app.globalData.logShangpin,
      ['shangpin[0].choose']: true,
      imgurl: app.globalData.logShangpin[0].shangpintiaoma,
      nowPrint: app.globalData.nowPrint != null ? app.globalData.nowPrint : []
    })
  },
  openPrinter(e) {
    console.log("openPrinter start");
    let item = {}
    if (wx.getStorageSync('print')) {
      item = wx.getStorageSync('print')
    }
    if (e && e.currentTarget.dataset.item) {
      item = e.currentTarget.dataset.item
    }
    let self = this;
    // let name = "S6-D726050099"
    wx.showLoading({
      title: '连接中',
      mask: true
    });
    JCAPI.openPrinter(item.name, function () {
      wx.showToast({
        title: '连接打印机成功',
        icon: '',
        image: '',
        duration: 2000,
        mask: true,
        success: function (res) {
          wx.hideLoading();
          let Printer = self.data.Plist;
          var arr = Printer.filter(function (items) {
            return items.name !== item.name;
          });
          wx.setStorageSync('print', item)
          getApp().globalData.nowPrint = item
          self.setData({
            nowPrint: item,
            Plist: arr
          })
        },
      })
      setTimeout(() => {
        console.log("hello World");
      }, 2000)
    }, function () {
      self.setData({
        nowPrint: ""
      })
      wx.removeStorageSync("print")
      wx.showToast({
        title: '连接失败',
        icon: 'error',
        duration: 2000
      })
    });
  },
  disconnect: function (e) {
    console.log("disconnect start");
    let self = this
    // wx.hideLoading()
    wx.showToast({
      title: '打印机连接断开',
      icon: '',
      image: '',
      duration: 2000,
      mask: true,
      success: function (res) {
        self.setData({
          nowPrint: ""
        })
        wx.removeStorageSync("print")
        // self.scanedPrinters()
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    JCAPI.closePrinter();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})